package frontend;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import backend.GodMode;

//VS4E -- DO NOT REMOVE THIS LINE!
public class SetCashDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private BoardFrame boardFrame;
	private GodMode godMode;
	private GodModeDialog testerModeDialog;
	private JTextField cashValue;
	private JButton okButton;
	private JLabel currentPlayerCash;
	private JLabel backgroundImage;
	private JButton cancelButton;
	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
	public SetCashDialog() {
		initComponents();
	}

	public SetCashDialog(GodModeDialog parent, GodMode godMode, BoardFrame boardFrame) {
		super(parent, true);
		initComponents();
		//this.godMode = godMode;
		this.boardFrame = boardFrame;
		this.testerModeDialog = parent;
	}
	
	public GodMode getGodMode() {
		return this.godMode;
	}
	
	public void setGodMode(GodMode godMode) {
		this.godMode = godMode;
	}
	
	private void initComponents() {
		setLocationRelativeTo(this.testerModeDialog);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setTitle("Set Dice Value");
		setBackground(Color.white);
		setResizable(false);
		setForeground(Color.black);
		setLayout(new GroupLayout());
		add(initCurrentPlayerCash(), new Constraints(new Leading(113, 10, 10), new Leading(16, 10, 10)));
		add(initOkButton(), new Constraints(new Leading(180, 56, 6, 6), new Leading(64, 6, 6)));
		add(initCashValue(), new Constraints(new Leading(111, 126, 6, 6), new Leading(34, 10, 10)));
		add(initCancelButton(), new Constraints(new Leading(111, 6, 6), new Leading(64, 6, 6)));
		add(initBackgroundImage(), new Constraints(new Leading(0, 6, 6), new Leading(0, 6, 6)));
		addWindowListener(new WindowAdapter() {
	
			public void windowClosing(WindowEvent event) {
			}
		});
		setSize(269, 140);
		setLocation(255,218);
	}

	private JLabel initCurrentPlayerCash() {
		if (currentPlayerCash == null) {
			currentPlayerCash = new JLabel();
			currentPlayerCash.setFont(new Font("Calibri", Font.PLAIN, 15));
			currentPlayerCash.setText("Current Player Cash");
		}
		return currentPlayerCash;
	}
	private JTextField initCashValue() {
		if (cashValue == null) {
			cashValue = new JTextField();
			cashValue.setAutoscrolls(true);
		}
		return cashValue;
	}
	private JButton initOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					okActionActionPerformed(event);
				}
			});
		}
		return okButton;
	}
	private JButton initCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					cancelActionActionPerformed(event);
				}
			});
		}
		return cancelButton;
	}
	private JLabel initBackgroundImage() {
		if (backgroundImage == null) {
			backgroundImage = new JLabel();
			backgroundImage.setIcon(new ImageIcon(getClass().getResource("/images/godMode/InputCashValue.jpg")));
		}
		return backgroundImage;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				SetCashDialog dialog = new SetCashDialog();
				dialog
						.setDefaultCloseOperation(SetCashDialog.DISPOSE_ON_CLOSE);
				dialog.setTitle("InputDiceValueDialog");
				dialog.setLocationRelativeTo(null);
				dialog.getContentPane().setPreferredSize(dialog.getSize());
				dialog.pack();
				dialog.setVisible(true);
			}
		});
	}

	private void okActionActionPerformed(ActionEvent event) {
		String cashString = this.cashValue.getText();
		if(!cashString.equals("")) {
			try {
				int cashValue = Integer.parseInt(cashString);
			
				this.boardFrame.getCurrentPlayer().setCash(cashValue);
				this.boardFrame.resetCashDisplay();
				this.boardFrame.resetMenuButtons();
				this.dispose();
				
			}
			catch(NumberFormatException e) {
			}
		}
		else {
			JOptionPane.showMessageDialog(null, "Ooops!!! \nYou have not yet entered a number. \nPlease a proper integer value.", "Ooops!!!", JOptionPane.PLAIN_MESSAGE);
		}
	}

	private void cancelActionActionPerformed(ActionEvent event) {
		this.dispose();
	}

}
